package Test1;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.SwingConstants;

public class SinClient extends JFrame implements Runnable {

	Socket socket;
	BufferedWriter send = null;
	BufferedReader receive =null;
	boolean threadFlag = false;
	StringBuilder data = new StringBuilder();
	
	private JPanel contentPane;
	private JPanel panel;
	private JPanel panel_1;
	private JButton bt1;
	private JButton bt;
	private JTextField ip;
	private JButton bt2;
	private JTextField message;
	private JScrollPane scrollPane;
	private JTextPane result;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SinClient frame = new SinClient();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SinClient() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getPanel(), BorderLayout.NORTH);
		contentPane.add(getPanel_1(), BorderLayout.SOUTH);
		contentPane.add(getScrollPane(), BorderLayout.CENTER);
		
		try {
			InetAddress ia = InetAddress.getLocalHost();
			setTitle("서버: IP" + ia.getHostAddress() + ", port: " + "4444");

		} catch (Exception e) {

		}
		
	}

	public void sendMsg(String s) {
		try {
			send.write(s);
			send.newLine();
			send.flush();
			dataSet("<font color = 'blue'> 클라이언트 :" + s + "</font>");
		} catch (Exception e) {

		}
	}

	public void serverStart() {
		try {
			Thread t = new Thread(this);
			t.start();
			
			bt.setEnabled(false);
			bt1.setEnabled(true);
			
			dataSet("<b>서버가 시작 되었네 그려~ </b>");	
		} catch (Exception e) {

		}
	}

	public void serverStop() {
		bt.setEnabled(true);
		bt1.setEnabled(false);
		threadFlag = false; 
		
		try {

		} catch (Exception e) {

		}

	}

	public void dataSet(String d) {
		try {
			data.append(d+"<br/>");
			result.setText(data.toString());

		} catch (Exception e) {

		}

	}

	public void run() {
		try {
			socket = new Socket(ip.getText(),4444);
			
			OutputStream os = socket.getOutputStream();
			OutputStreamWriter osw = new OutputStreamWriter(os);
			send = new BufferedWriter(osw);
			
			InputStream is = socket.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			receive = new BufferedReader(isr);

			threadFlag = true;
			
			while(threadFlag){
				String str = receive.readLine();
				dataSet("수신: " + str);
				
			}
			
			dataSet("<font color = 'red' > 서버가 중지 되었네 그려~ </font>");
			bt.setEnabled(true);
			bt1.setEnabled(false);
			
			receive.close();
			isr.close();
			is.close();
			
			send.close();
			osw.close();
			os.close();
			
			socket.close();
			
		} catch (Exception e) {

		}

	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getIp());
			panel.add(getBt(), BorderLayout.WEST);
			panel.add(getBt1(), BorderLayout.EAST);
		}
		return panel;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setLayout(new BorderLayout(0, 0));
			panel_1.add(getMessage());
			panel_1.add(getBt2(), BorderLayout.EAST);
		}
		return panel_1;
	}
	private JButton getBt1() {
		if (bt1 == null) {
			bt1 = new JButton("해제");
			bt1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					serverStop();
				}
			});
		}
		return bt1;
	}
	private JButton getBt() {
		if (bt == null) {
			bt = new JButton("접속");
			bt.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					serverStart();
				}
			});
		}
		return bt;
	}
	private JTextField getIp() {
		if (ip == null) {
			ip = new JTextField();
			ip.setHorizontalAlignment(SwingConstants.CENTER);
			ip.setText("192.168.10.130");
			ip.setColumns(10);
		}
		return ip;
	}
	private JButton getBt2() {
		if (bt2 == null) {
			bt2 = new JButton("전송");
			bt2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String str = message.getText();
					sendMsg(str);
					message.setText("");
				}
			});
		}
		return bt2;
	}
	private JTextField getMessage() {
		if (message == null) {
			message = new JTextField();
			message.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e) {
					if(e.getKeyCode() == KeyEvent.VK_ENTER){
					String str = message.getText();
					sendMsg(str);
					message.setText("");
					}
				}
			});
			message.setColumns(10);
		}
		return message;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}
	private JTextPane getResult() {
		if (result == null) {
			result = new JTextPane();
			result.setContentType("text/html");
		}
		return result;
	}
}
